<template>
	<u-popup v-model="showPop" mode="center" closeable @close="popClose">
		<view class="pop-view">
			<view class="pop-search">
				<view class="line-view" style="justify-content: space-between">
					<view class="line-halfway">
						<uni-easyinput v-model="search.name" border placeholder="物料名称"></uni-easyinput>
					</view>
					<view class="line-halfway">
						<uni-easyinput v-model="search.code" border placeholder="物料代码"></uni-easyinput>
					</view>
				</view>
				<view class="line-view" style="align-items: center; padding: 0">
					<view class="line-halfway">
						<uni-easyinput v-model="search.productModel" border placeholder="规格型号"></uni-easyinput>
					</view>
					<view class="line-search" @click="$u.throttle(doSearch, 1500)">
						<uni-icons type="search" color="#a7a7a7" size="18"
							style="margin-bottom: -5rpx; margin-right: 5rpx"></uni-icons>
						搜索
					</view>
				</view>
			</view>

			<view class="pop-info">
				<scroll-view style="height: 100%" :scroll-top="scrollTop" scroll-y="true" class="scroll-Y">
					<view class="pop-card" :class="{ isCheck: item.check == 1 }" v-for="(item, index) of tableData" :key="index"
						@click="addInfo(index)">
						<view class="popCard-name">
							<view class="pop-title">
								{{ item.name }}
							</view>
							<view class="pop-check">
								<uni-icons v-if="item.check == 1" color="#3067E8" type="checkbox-filled" size="22" />
								<uni-icons v-else color="#484848" type="circle" size="22" />
							</view>
						</view>
						<view class="popCard-line">
							<view class="popCard-li-label">物料编码</view>
							<view class="popCard-li-value">
								{{ item.code }}
							</view>
						</view>
						<view class="popCard-line">
							<view class="popCard-li-label">规格型号</view>
							<view class="popCard-li-value">
								{{ item.productModel }}
							</view>
						</view>
						<view class="popCard-line">
							<view class="popCard-li-label">计量单位</view>
							<view class="popCard-li-value">
								{{ item.unitName }}
							</view>
						</view>
						<view class="popCard-line" v-if="item.auxUnitId">
							<view class="popCard-li-label">辅助单位</view>
							<view class="popCard-li-value">
								{{ item.auxUnitName }}
							</view>
						</view>
						<view class="popCard-line">
							<view class="popCard-half">
								<view class="popCard-li-label">批号</view>
								<view class="popCard-li-valueLittle">
									{{ item.batch || '--' }}
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>

			<view class="pop-bottom">
				<uni-pagination :total="search.total" :pageSize="search.perPage" @change="pageChange" />
			</view>
		</view>
	</u-popup>
</template>

<script>
	var that;
	export default {
		props: {
			popShow: Boolean,
			projectInfo: Array,
			factoryId: String
		},
		watch: {
			popShow(newVal) {
				this.showPop = newVal; // 监听 props 的变化，更新 data 属性的值
			},
			projectInfo(newVal) {
				var list = that.$u.deepClone(that.tableData);
				for (const item of list) {
					item.check = 0;
				}
				// 搜索后 如果存在已选中数组 对比添加选中状态
				if (newVal.length > 0) {
					for (const infoItem of newVal) {
						for (const listItem of list) {
							if (infoItem.id === listItem.id) {
								listItem.check = 1;
								break;
							}
						}
					}
				}

				that.tableData = that.$u.deepClone(list);
			},
		},
		data() {
			return {
				showPop: this.popShow,
				scrollTop: 0,
				tableData: [],
				addInfoList: [],
				search: {
					code: null,
					name: null,
					total: 0,
					page: 1,
					size: 20,
					productModel: null,
				},
			};
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		mounted() {
			that = this;
			that.doSearch();
		},
		methods: {
			clearnSearch() {
				that.search = {
					code: null,
					name: null,
					total: 0,
					page: 1,
					size: 20,
					productModel: null,
				};
				that.doSearch();
			},
			popClose() {
				this.$emit('popChange', false);
			},
			// 点击添加物料
			addInfo(e) {
				const list = that.$u.deepClone(that.tableData);
				for (const item of that.projectInfo) {
					if (list[e].id == item.id) {
						that.$u.toast('该物料已添加');
						return false;
					}
				}
				list[e].check = 1;
				that.tableData = that.$u.deepClone(list);
				const now = new Date();
				const year = now.getFullYear();
				const month = now.getMonth() + 1;
				const date = now.getDate();
				let receiptDate = year + '-' + month + '-' + date;
				list[e].receiptDate = receiptDate;
				// list[e].stockPositionId = that.stockInfo.id;
				// list[e].stockId = that.stockInfo.stockId;
				// list[e].stockAreaId = that.stockInfo.stockAreaId;
				list[e].productId = list[e].id;
				list[e].productCode = list[e].code;
				console.log(list);
				that.projectInfo.push(list[e]);
			},
			// 查询物料
			doSearch() {
				uni.showLoading({
					mask: true,
					title: '查询中...',
				});
				//分页-1 看一下search原数据
				const search = that.$u.deepClone(that.search);
				search.factoryId = that.factoryId;
				search.page = search.page - 1;
				that.$u.get('/api/product', search).then((res) => {
					if (res.code != 0) {
						uni.hideLoading();
						this.$u.toast(res.message);
						return false;
					}
					const list = res.data.content;
					for (const item of res.data.content) {
						item.check = 0;
					}

					// 搜索后 如果存在已选中数组 对比添加选中状态
					if (that.projectInfo.length > 0) {
						for (const infoItem of that.projectInfo) {
							for (const listItem of list) {
								if (infoItem.id === listItem.id) {
									listItem.check = 1;
									break;
								}
							}
						}
					}

					that.tableData = list;
					that.search.total = Number(res.data.totalElements);
				});
			},
			pageChange(e) {
				const {
					type,
					current
				} = e;
				that.search.page = current;
				that.doSearch();
			},
		},
	};
</script>

<style lang="scss">
	.pop-view {
		height: 100vh;
		width: 750rpx;
		padding: 45rpx 10rpx 5rpx 10rpx;
		display: flex;
		font-size: 35rpx;
		flex-direction: column;
		box-sizing: border-box;

		.pop-search {
			padding: 15rpx;
		}

		.pop-info {
			flex: 1;
			overflow: hidden;
			padding: 0 15rpx 15rpx 15rpx;
			background-color: #f8f8f8;

			.pop-card {
				padding: 25rpx;
				background-color: #fff;
				margin: 15rpx 0;
				border-radius: 15rpx;
				font-size: 30rpx;
				border: 1rpx solid #fff;

				view {
					margin: 2rpx 0;
				}

				.popCard-name {
					color: #000;
					font-size: 35rpx;
					margin-bottom: 10rpx;
					display: flex;

					.pop-title {
						width: 700rpx;
					}
				}

				.popCard-line {
					display: flex;
					align-items: center;

					.popCard-li-label {
						width: 160rpx;
						color: #999;
					}

					.popCard-half {
						display: flex;
					}

					.popCard-li-value {
						width: 500rpx;
						color: #555;
					}

					.popCard-li-valueLittle {
						width: 210rpx;
						color: #555;
					}
				}
			}

			.isCheck {
				border: 1rpx solid #9ca6ff;
			}
		}

		.pop-bottom {
			height: 80rpx;
			padding: 10rpx 25rpx 0 25rpx;
		}
	}

	.line-view {
		width: 100%;
		display: flex;
		align-items: flex-start;
		padding: 15rpx 0;

		.line-halfway {
			width: 345rpx;
		}

		.line-search {
			display: flex;
			align-items: center;
			padding-left: 25rpx;
			color: #a7a7a7;
			font-size: 32rpx;
		}

		.line-search:active {
			font-weight: 700;
		}

		.line-text {
			width: 150rpx;
			font-weight: 700;
			color: #555;
		}

		.line-value {
			width: 550rpx;
			display: flex;
			color: #6a6a6a;
		}
	}

	.project-views {
		width: 720rpx;
		padding-left: 10rpx;
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;

		view {
			display: flex;
		}
	}
</style>